package com.projectx.action.contact;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.util.ActivityManage;

public class FindFriend extends AppCompatActivity {
    private Button back;
    private ImageView delete;
    private EditText editText;
    private RecyclerView recyclerView;
    protected static String text;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManage.addActivity("Findfriend", this);
        setContentView(R.layout.activity_findfriend);

        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e){
            Log.d("KMH","没有状态栏标题");
        }
        back=findViewById(R.id.back);
        delete=findViewById(R.id.delete);
        editText=findViewById(R.id.edittext);
        recyclerView=findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final FindFriendAdapter findFriendAdapter =new FindFriendAdapter(this, text);
        recyclerView.setAdapter(findFriendAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        editText.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }//文本改变之前执行

            @Override
            //文本改变的时候执行
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                findFriendAdapter.notifyDataSetChanged();
                //如果长度为0
                if (s.length() == 0) {
                    //隐藏“删除”图片
                    delete.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.GONE);
                } else {//长度不为0
                    //显示“删除图片”
                    delete.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);
                    text=editText.getText().toString();
                }
            }

            public void afterTextChanged(Editable s) {
            }//文本改变之后执行
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               ActivityManage.exitActivity("Findfriend");
            }
        });

    }
}
